package org.mtmi.ui.controls;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.scenes.AnimationManager;
import org.mtmi.ui.scenes.IAnimated;
import org.mtmi.ui.scenes.Scene;

/**
 * Animated button used by {@link Menu}
 */
public class MenuItemButton  extends Button implements IAnimated
{

	public MenuItemButton(Scene scene, int flags ) {
		super( scene, flags );
		
	}
	
	void animationStart( boolean appear ) {
		if( AnimationManager.getDefault().add( this ) ) {
			animStep = appear ? 1 : 200 ;
		}
	}

	@Override
	public boolean animationStep() {
		if( animStep >= 300 ) {
			animStep = 0;
		}
		else if( animStep >=200 ) {
			animStep += 10;
			if( animStep >300 ) animStep=300;
		}
		else if( animStep>= 100 ) {
			animStep = 0;
		}
		else if( animStep > 0 ) {
			animStep += 15;
			if( animStep > 100 ) animStep = 100;
		}
		
		return animStep!=0;
	}
	
	@Override
	public boolean isAnimating() {
		return animStep!=0;
	}

	@Override
	public void paint(GC gc) {
		
		if( animStep==0 ) {
			//no animation
			super.paint(gc);
		} else {
			//note: animate width here, but as Button also used animStep to animate toggle swtich, color will be animated too
			Point size = getSize();
			Point animSize = new Point( size.x, size.y ); 
			if( animStep>0 && animStep < 200 ) {
				//appear: width from 0 to full size
				animSize.x = (size.x*animStep)/100;
			} else {
				//disappear: for animStep from 200 to 300 alpha from 255 to 0
				gc.setAlpha( 255 - (255*(animStep-200))/100 );
			}
			setSize( animSize );
			super.paint(gc);
			//restore full size:
			setSize( size );
			gc.setAlpha( 255 );
		}
	}
}
